<template>
  <el-dialog :title="showTitle" :visible="isDialog" @close="cancelBtn">
    <el-form ref="deptsForm" label-width="120px" :model="treeForm" :rules="treeRules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="treeForm.name" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="treeForm.code" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="treeForm.manager" placeholder="请选择" @focus="getUserManage">
          <el-option v-for="item in peoples " :key="item.id" :label="item.username" :value="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="treeForm.introduce" placeholder="1-300个字符" type="textarea" rows="3" />
      </el-form-item>
    </el-form>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button size="small" @click.native="cancelBtn">取消</el-button>
        <el-button type="primary" size="small" @click.native="confirmBtn">确认</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getDepartment, getUserDepts, addDepartment, getDepartmentId, updateDepartment } from '@/api/department'
export default {
  name: '',
  components: {

  },
  props: {
    isDialog: {
      type: Boolean,
      required: true
    },
    treeNode: {
      type: Object,
      default: null
    }
  },
  data() {
    const checkNameRepeat = async(rule, value, callback) => {
      const { depts } = await getDepartment()
      let isDepts = false
      if (this.treeForm.id) {
        //   筛选出所有的同级部门并除自己以外
        isDepts = depts.filter(item => item.pid === this.treeNode.pid && item.id !== this.treeNode.id).some(item => item.name === value)
      } else {
        isDepts = depts.filter(item => item.pid === this.treeNode.id).some(item => item.name === value)
      }

      isDepts ? callback(new Error(`该${value}部门已经存在`)) : callback()
    }
    const checkCodeRepeat = async(rule, value, callback) => {
      const { depts } = await getDepartment()
      let isDepts = false
      if (this.treeForm.id) {
        isDepts = depts.filter(item => item.id !== this.treeNode.id).some(item => item.code === value && value)
      } else {
        isDepts = depts.some(item => item.code === value)
      }

      isDepts ? callback(new Error(`该${value}编码已经存在`)) : callback()
    }
    return {
      treeForm: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      treeRules: {
        name: [{ required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称的长度为1-50个字符', trigger: 'blur' },
          { trigger: 'blur', validator: checkNameRepeat }
        ],
        code: [{ required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码的长度为1-50个字符', trigger: 'blur' },
          { trigger: 'blur', validator: checkCodeRepeat }
        ],
        manager: [{ required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        introduce: [{ required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '部门介绍的长度为1-300个字符', trigger: 'blur' }
        ]
      },
      peoples: []
    }
  },
  computed: {
    showTitle() { return this.treeForm.id ? '编辑部门' : '新增子部门' }
  },
  watch: {},
  created() {

  },
  mounted() {},
  methods: {
    async getUserManage() {
      const result = await getUserDepts()
      console.log(result)
      this.peoples = result
    },
    async getUserDepartment(id) {
      this.treeForm = await getDepartmentId(id)
    },
    confirmBtn() {
      this.$refs.deptsForm.validate(async isOk => {
        if (isOk) {
          // 判断是否有id
          if (this.treeForm.id) {
            await updateDepartment(this.treeForm)
          } else {
            await addDepartment({ ...this.treeForm, pid: this.treeNode.id })
          }
          this.$emit('addDepts')
          this.$emit('update:isDialog', false)
        }
      })
    },
    cancelBtn() {
      this.treeForm = {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      }
      this.$emit('update:isDialog', false)
      this.$refs.deptsForm.resetFields()
    }
  }
}
</script>

<style scoped>

</style>
